package com.whfc.emp.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.common.exception.BizException;
import com.whfc.emp.service.AppWorkPlanService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * @Description: 分级推送报警信息定时任务
 * @version: 1.0
 * @date: 2024-01-01
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class GuardianAlarmJob {

    private static final Logger logger = LoggerFactory.getLogger(GuardianAlarmJob.class);

    @Autowired
    private AppWorkPlanService appWorkPlanService;

    /**
     * 分级推送报警信息
     */
    @XxlJob("guardianAlarmPush")
    public void guardianAlarmPush() {
        long startTime = System.currentTimeMillis();
        try {
            logger.info("开始执行分级推送报警信息定时任务");

            boolean result = appWorkPlanService.sendAlarmMessageToWorkPlanGuardians();
            
            long costTime = System.currentTimeMillis() - startTime;
            if (result) {
                logger.info("分级推送报警信息定时任务执行完成，耗时：{}ms", costTime);
            } else {
                logger.warn("分级推送报警信息定时任务执行完成，但未处理任何报警，耗时：{}ms", costTime);
            }
        } catch (BizException e) {
            logger.error("分级推送报警信息定时任务业务异常：{}", e.getMessage(), e);
        } catch (Exception e) {
            logger.error("分级推送报警信息定时任务执行异常", e);
        }
    }
}